package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkhub.oms.common.enums.mp.RoleCodeEnum;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mapper.UserRoleEntityMapper;
import com.linkhub.oms.dao.mpservice.IUserRoleService;
import com.linkhub.oms.entity.UserRoleEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-05
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleEntityMapper, UserRoleEntity> implements IUserRoleService {

    @Override
    public void saveUserRole(String userId, RoleCodeEnum roleCode) {
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUserId(userId);
        userRoleEntity.setRoleCode(roleCode);
        userRoleEntity.setCreatedTime(LocalDateTime.now());
        userRoleEntity.setUpdateTime(LocalDateTime.now());

        super.save(userRoleEntity);

    }

    @Override
    public List<String> listUserRoleCodes(String userId) {

        return QtStreamUtil.map(super.lambdaQuery()
                        .select(UserRoleEntity::getRoleCode)
                        .eq(UserRoleEntity::getUserId, userId)
                        .list(),
                entity -> entity.getRoleCode().getCode());
    }
}
